package org.springblade.common.tool;

import cn.hutool.json.JSONUtil;
import org.apache.commons.codec.binary.Hex;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class MASUtil {
	static String ecName="咸宁市政府服务和大数据管理";
	static String apId="qxxl_test";
	static String secretKey="1qaz@WSX";
	static String sign="SSz7V1fnN";
	static String addSerial="";
	static String url="http://112.35.1.155:1992/sms/norsubmit";
	public static String SendSms(Map<String,String> params) throws Exception{
		params.put("ecName",ecName);
		params.put("apId",apId);
		params.put("secretKey",secretKey);
		params.put("sign",sign);
		params.put("addSerial",addSerial);

		StringBuffer stringBuffer=new StringBuffer();
		stringBuffer.append(params.get("ecName"));
		stringBuffer.append(params.get("apId"));
		stringBuffer.append(params.get("secretKey"));
		stringBuffer.append(params.get("mobiles"));
		stringBuffer.append(params.get("content"));
		stringBuffer.append(params.get("sign"));
		stringBuffer.append(params.get("addSerial"));
		MessageDigest md = MessageDigest.getInstance("MD5");
		params.put("mac", Hex.encodeHexString(md.digest(stringBuffer.toString().getBytes(StandardCharsets.UTF_8))));
		String param= JSONUtil.toJsonStr(params);
		String encode= Base64.getEncoder().encodeToString(param.getBytes(StandardCharsets.UTF_8));
		return HttpUtil.doPost(url,encode,"json");
	}
	public static void main(String[] args) throws Exception{
		Map<String,String> map=new HashMap<>();
		map.put("mobiles","17362412346");
		map.put("content","【咸宁市政府】您的验证码是：123456");
		try {
			String s = SendSms(map);
			System.out.println(s);
		}catch (Exception e){

		}
	}
}
